package com.gupao.p7store.common.utils.format;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;


/**
 * desc: 美化工具
 *
 * @ClassName: com.gupao.p7store.ota.protocol.utils.format.BeautyUtils
 * @Author: dengbicheng
 * @CreateTime: 2021-12-09  22:59:16
 * @Version: v 1.0.0
 */
@Slf4j
public class BeautyUtils {

    public static String pretty(String json) {
        String prettyJson = json;
        if (StringUtils.isEmpty(json)) {
            return StringUtils.EMPTY;
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(json);
            prettyJson = pretty(jsonObject);
        } catch (JSONException e) {
            return prettyJson;
        }
        try {
            JSONArray jsonArray = JSONObject.parseArray(json);
            prettyJson = pretty(jsonArray);
        } catch (JSONException e) {
            return prettyJson;
        }
        return prettyJson;
    }

    public static <T> String pretty(T t) {
        if (ObjectUtils.isEmpty(t)) {
            return StringUtils.EMPTY;
        }
        return JSONArray.toJSONString(t, SerializerFeature.PrettyFormat,
            SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
    }
}
